<?php
defined('IN_XLP') or exit('Access Denied!');

class wmadmin {

    public static $adminInfo = array('id' => 0, 'account' => '', 'shop_id' => '', 'is_super' => 0, 'status' => 0);

    static function getUserById($id) {
        static $admins = array();
        if (!isset($admins[$id]) || empty($admins[$id])) {
            $admins[$id] = D('wmadmin')->getUserInfoById($id);
        }
        return $admins[$id];
    }

    /* 获取当前登录用户信息
     * @param string 需要获取的字段
     * @return array or string
     */
    static function getLoginUser($field = '', $getData = '') {
        if (!self::$adminInfo['id']) {
            $admin = array();
            if (!$getData) {
                $getData = array('auth' => myCookie('wms_auth'), 'saltkey' => myCookie('wms_saltkey'));
            }
            if ($getData['auth'] && $getData['saltkey']) {
                $getData['auth'] = explode("\t", getDecode($getData['auth'], self::getAuthKey($getData['saltkey'])));
                list($id, $pwd)  = empty($getData['auth']) || count($getData['auth']) < 2 ? array(0, '') : $getData['auth'];
                if ($id) {
                    $admin = self::getUserById($id);
                }
                if ($admin && $admin['pwd'] == $pwd) {
                    self::$adminInfo = $admin;
                }
            }
        }
        return $field && isset(self::$adminInfo[$field]) ? self::$adminInfo[$field] : self::$adminInfo;
    }

    static function getAuthKey($saltkey) {
        return md5(C('System', 'vcode') . md5($saltkey));
    }

    static function setUserLogOut() {
        myCookie('wms_auth', null);
        myCookie('wms_saltkey', null);
        $_COOKIE['wms_auth'] = null;
        $_COOKIE['wms_saltkey'] = null;
        self::$adminInfo = array('id' => 0, 'account' => '', 'shop_id' => '', 'is_super' => 0, 'status' => 0);
        return true;
    }

}